using System;
using System.IO;
using System.Data;
using System.Net;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class FTPUpload : MissionPlugin
    {
        [Package(Direction.Input, "Defines the access settings for a ftp server.")]
        enum FTP_Settings
        {
            [Column(typeof(string), "The directory, where you like to upload files.")]
            DirectoryURLPath,
            [Column(typeof(string), "The user name associated with the ftp server.")]
            UserName,
            [Column(typeof(string), "The password for the user name associated with the ftp server.")]
            Password
        }

        [Package(Direction.Input, "Defines all files you like to upload.")]
        enum UploadFiles
        {
            [Column(typeof(string), "The file you like to upload from your desktop machine.",
                Validation = "isexistingfile")]
            FilePath
        }

        [Plugin(PluginDevStatus.Production, "Upload the given files to the expected FTP-server.")]
        public FTPUpload()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {
            // get the ftp settings
            DataTableReader ftpSettings = this.GetDataReader(typeof(FTP_Settings));
            while (ftpSettings.Read())
            {
                this.AddNewLoggingMessage(typeof(FTP_Settings), String.Format("connecting with ftp server: '{0}'", ftpSettings[FTP_Settings.DirectoryURLPath.ToString()].ToString()));
                
                // get all upload file
                DataTableReader uploadFiles = this.GetDataReader(typeof(UploadFiles));
                while (uploadFiles.Read())
                {
                    FileInfo fileToUpload = new FileInfo(uploadFiles[UploadFiles.FilePath.ToString()].ToString());

                    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpSettings[FTP_Settings.DirectoryURLPath.ToString()].ToString() + "/" + fileToUpload.Name);
                    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                    ftpRequest.Credentials = new NetworkCredential(ftpSettings[FTP_Settings.UserName.ToString()].ToString(), ftpSettings[FTP_Settings.Password.ToString()].ToString());
                    ftpRequest.UsePassive = true;
                    ftpRequest.UseBinary = true;
                    ftpRequest.KeepAlive = false;                    
                    
                    this.AddNewLoggingMessage(typeof(UploadFiles), String.Format("upload file '{0}'", uploadFiles[UploadFiles.FilePath.ToString()].ToString()));

                    FileStream stream = File.OpenRead(uploadFiles[UploadFiles.FilePath.ToString()].ToString());
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    stream.Close();

                    Stream reqStream = ftpRequest.GetRequestStream();
                    reqStream.Write(buffer, 0, buffer.Length);
                    reqStream.Close();

                    //FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
                    //this.AddNewLoggingError(String.Format("Append status: {0}", response.StatusDescription));
                    //response.Close();  

                    ftpRequest.Abort();
                }
            }
        }  
    }
}
